/**
 * @author zhoutao
 *  2010-11-6
 */
package com.jlt.tag;

import java.io.IOException;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;

import org.apache.commons.lang.StringUtils;

import com.jlt.entity.dic.DicEntity;
import com.jlt.service.dic.DicEntityManager;

/**
 * @author zhoutao
 * 
 */
public class TextTag extends BaseTag {

	private static final long serialVersionUID = 1L;

	private String dicCode;
	private String type;
	private Long dicId;

	private DicEntityManager dicEntityManager;

	@Override
	public int doStartTag() throws JspException {

		dicEntityManager = (DicEntityManager) getManagerBean("dicEntityManager");

		if ("STATUS".equalsIgnoreCase(type)) {
			stausText();
		} else if ("DIC".equalsIgnoreCase(type)) {
			dicText();
		}

		return SKIP_BODY;
	}

	public void stausText() {

		JspWriter out = pageContext.getOut();
		try {
			if ("A".equals(dicCode)) {
				out.print("可用");
			} else if ("N".equals(dicCode)) {
				out.print("" + "不可用");
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	public void dicText() {

		if (StringUtils.isNotBlank(dicCode)) {
			DicEntity dicEntity = dicEntityManager.getDicEntityByCode(dicCode);
			if (null != dicEntity) {
				JspWriter out = pageContext.getOut();
				try {
					out.print(dicEntity.getName());
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		} else if (null != dicId) {
			DicEntity dicEntity = dicEntityManager.getDicEntity(dicId);
			if (null != dicEntity) {
				JspWriter out = pageContext.getOut();
				try {
					out.print(dicEntity.getName());
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
	}

	@Override
	public int doEndTag() throws JspException {
		return super.doEndTag();
	}

	public String getDicCode() {
		return dicCode;
	}

	public void setDicCode(String dicCode) {
		this.dicCode = dicCode;
	}

	public Long getDicId() {
		return dicId;
	}

	public void setDicId(Long dicId) {
		this.dicId = dicId;
	}

	public String getType() {
		return type;
	}

	public void setType(String type) {
		this.type = type;
	}
}
